// pages/todayAppointments/todayAppointments.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    //显示数据
    assistant: [],
    assistant1: [{
      logoLink: '/images/defaultImage.png',
      assistantName: '小红',
      assistantTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      assistantName: '小明',
      assistantTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      assistantName: '小白',
      assistantTel: "18727508188"
    },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();
    this.setData({
      username: getApp().globalData.username,
    })
    this.getAssistant()
    
    try {
      var ap = wx.getStorageSync('assistant')
      this.setData({
        assistant: ap
      })
    } catch(e) { }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.getAssistant()
    try {
      var ap = wx.getStorageSync('assistant')
      this.setData({
        assistant: ap
      })
    } catch(e) { }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAssistant()
  },

  /**
   * 获取助理信息
   */
  getAssistant: function () {
    var that = this
    wx.request({
      url: this.data.url + 'getAssistantListServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorID: getApp().globalData.username
      },
      success: function (res) {
        console.log(res.data)
        if (res.data != false && res.data != "False" && res.data != "空") {
          try {
            wx.setStorageSync("assistant", res.data)
          } catch (e) { }
          that.setData({
            assistant: res.data
          })
        }
        else if(res.data == "空" || res.data=="False") {
          that.setData({
            assistant: null
          })
        }
        else {
          wx.showToast({
            title: '获取失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 医生点击添加助理
   */
  addAssistant: function(e) {
    wx.navigateTo({
      url: '../addAssistant/addAssistant',
    })
  },

  /**
   * 医生点击删除助理
   */
  deleteAssistant: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    var assistantTel = this.data.assistant[index].assistantTel
    wx.request({
      url: this.data.url + 'deleteAssistantServlet',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorID: getApp().globalData.username,
        assistantTel: assistantTel
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data != "空") {
          wx.showToast({
            title: '您已成功删除该助理',
            icon: 'none',
            duration: 2000
          })
          that.getAssistant()
        }
        else {
          wx.showToast({
            title: '删除失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络连接失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 医生点击与助理交谈
   */
  chatWithAssistant: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var assistantTel = JSON.stringify(this.data.assistant[index].assistantTel)
    var assistantName = JSON.stringify(this.data.assistant[index].assistantName)
    wx.navigateTo({
      url: '../onlineConsulting/onlineConsulting?otherTel=' + assistantTel + '&&otherName=' + assistantName,
    })
  }
})